package io.bitmax.library.mvp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;
import io.bitmax.library.utils.LogUtils;

/**
 * Created by gongdongyang on 2018/9/18.
 */

public abstract class BaseCoreFragment extends RxFragment {

    private View rootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtils.d(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d(getClass().getSimpleName() + "------enter------");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtils.i(getClass().getSimpleName() + "------enter------");

        if (rootView == null) {
            setRootView(createView(inflater, container, savedInstanceState));
            executeOnceAfterCreateView();
        }

        if (rootView.getParent() != null)
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        ButterKnife.bind(this,rootView);
        return rootView;
    }


    /**
     * 只执行一次
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    protected abstract void executeOnceAfterCreateView();


    protected abstract void initView();

    protected abstract void addEvent();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.i(getClass().getSimpleName() + "------enter------");
    }

    protected void setRootView(View rootView) {
        this.rootView = rootView;
    }

    public View getRootView() {
        return rootView;
    }

}
